<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View, Session};

use app\common\model\{Admin, Menu, User, Order};

class IndexController extends BaseController
{
    private $token;
    public function __construct(Admin $adminModel)
    {
        parent::initialize();
        $this->adminModel = $adminModel;
        $this->token = Session::get('token');
    }

    public function index()
    {
        $menuModel = new Menu();
        $a = Session::get('token');

        $where = ['pid' => 0, 'is_show' => 1];
        $menu = $menuModel->getList($where, 0, 0, 0, '*', 'sort DESC, id')['data'];

        foreach ($menu as $key => $value) {
            $menu[$key]['list'] = $menuModel->getList(['pid' => $value['id'], 'is_show' => 1], 0)['data'];
        }

        View::assign('menu', $menu);
        View::assign('admin', $this->token);
        return view();
    }

    public function home()
    {
        /** 会员数量 */
        $user_count = User::count();

        /** 销量 */
        $order_total = Order::where([['status', 'in', [2,3,4]]])->sum('total');

        View::assign('user_count', $user_count);
        View::assign('order_total', $order_total);
        View::assign('admin', $this->token);
        return view();
    }
}
